﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCSService;
using NLog;
namespace SCS.Core.Controllers
{
    //comment
    public class BaseController : Controller
    {
        private static readonly Logger Logger = LogManager.GetLogger("webmaster");

        public string DefaultHostGetter() { return this.Request.Url.Host; }

        public string DefaultPathAndQuerryGetter() { return this.Request.Url.PathAndQuery; }

        public string DefaultUrlRefferer() { return this.Request.UrlReferrer.ToString(); }

        public string DefaultLanguageGetter() {
            var arr = this.Request.Url.PathAndQuery.Split('/');
            return arr[2];
        }

        public int DefaultSiteGuidGetter() {
            var arr = this.Request.Url.PathAndQuery.Split('/');
            return int.Parse(arr[1]);
        }

        public int GetSiteGuid()
        {
            int siteGuid = 0;
            if (Request.Url.Host == "localhost")
            {
                if (Request.Url.ToString().Contains("favicon.ico"))
                {
                }
                else
                    siteGuid = DefaultSiteGuidGetter();
            }
            else
            {
                //Logger.Info("domain:" + DefaultHostGetter());

                siteGuid = DefaultSiteGuidGetter();

                //Logger.Info("siteguid:" + siteGuid);
            }
                


            return siteGuid;
        }

        public void UpdatePagerViewData(int totalPages, int totalRows, int currentPage, int pageSize)
        {
            ViewData["pagerStats"] = GenPagerStats(totalRows, currentPage, pageSize);
            ViewData["totalPageDrop"] = GenPageDrop(totalPages,currentPage);
            
            if (totalPages == 1)
            {
                ViewData["isPagerVisible"] = false;
                return;
            }

            int lastPage = totalPages;

            ViewData["isPagerVisible"] = true;
            ViewData["currentPage"] = currentPage;
            ViewData["totalPages"] = totalPages;


            if (currentPage == 1)
            {
                ViewData["isFirstPage"] = true;
            }
            else
            {
                ViewData["isFirstPage"] = false;
                ViewData["previousPage"] = currentPage - 1;
            }


            if (currentPage == lastPage)
            {
                ViewData["isLastPage"] = true;
            }
            else
            {
                ViewData["isLastPage"] = false;

                ViewData["nextPage"] = currentPage + 1;
                ViewData["lastPage"] = lastPage;
            }
        }

        public void UpdateSorterViewData(string sortField, string sortDirection)
        {
            ViewData["sortField"] = sortField;
            ViewData["sortDirection"] = sortDirection;
        }

        public static string GenPagerStats(int totalRows, int currentPage, int pageSize)
        {
            int firstRow = (currentPage - 1) * pageSize + 1;
            int lastRow = Math.Min(firstRow + pageSize - 1, totalRows);

            string stats;
            if (firstRow == lastRow)
                stats = "Row " + firstRow + " of " + totalRows;
            else
                stats = "Rows " + firstRow + "-" + lastRow + " of " + totalRows;

            return stats;
        }

        public static SelectList GenPageDrop(int totalPage,int currentPage)
        {

            var tp = new List<string> { };
            for (int i = 1; i < (totalPage + 1); i++)
            {
                tp.Add("" + i);
            }
            return new SelectList(tp,currentPage);
        }

        public static SelectList PageSizeSelectList()
        {
            var pageSizes = new List<string> { "20", "40", "80","100" };
            return new SelectList(pageSizes, "Value");
        }

    }
}
